namespace CayleyTree
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private Graphics graphics;
        private Color c;
        char error = 'a';
        private void button1_Click(object sender, EventArgs e)
        {
            int n = 0;
            double leng = 100;
            try
            {
                if(!int.TryParse(textBox1.Text,out n))
                {
                    error = 'a';
                    throw new Exception();
                }
                if(!double.TryParse(textBox2.Text,out leng))
                {
                    error = 'b';
                    throw new Exception();
                }
                if (graphics == null) graphics = this.CreateGraphics();
                drawCayleyTree(n, 200, 310, leng, -Math.PI / 2);
            }
            catch (Exception ex)
            {
                if(error == 'a')
                {
                    MessageBox.Show("递归深度请输入一个正整数");
                }
                else if(error == 'b')
                {
                    MessageBox.Show("主干长度请输入一个正数");
                }
            }
        }

        void drawCayleyTree(int n,double x0,double y0,double leng,double th)
        {
            double per1 = 1;
            double per2 = 1;
            double th1 = 90;
            double th2 = 90;
            try
            {
                if(!double.TryParse(textBox3.Text, out per1) || !double.TryParse(textBox4.Text, out per2))
                {
                    error = 'c';
                    throw new Exception();
                }
                if (!double.TryParse(textBox5.Text, out th1) || !double.TryParse(textBox6.Text, out th2))
                {
                    error = 'd';
                    throw new Exception();
                }
                if (n == 0) return;
                double x1 = x0 + leng * Math.Cos(th);
                double y1 = y0 + leng * Math.Sin(th);
                drawLine(x0, y0, x1, y1);
                drawCayleyTree(n - 1, x1, y1, per1 * leng, th + th1*Math.PI/180);
                drawCayleyTree(n - 1, x1, y1, per2 * leng, th - th2 * Math.PI / 180);
            } catch (Exception ex)
            {
                if(error == 'c')
                {
                    MessageBox.Show("分支长度比请输入一个正数");
                }
                else if(error == 'd')
                {
                    MessageBox.Show("分支角度请输入一个数字");
                }
            }
        }

        private Pen CustomizePenColor = new Pen(Color.FromArgb(0,0,0));
        void drawLine(double x0,double y0, double x1, double y1)
        { 
            graphics.DrawLine(
            CustomizePenColor,(int)x0,(int)y0,(int)x1,(int)y1);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            graphics.Clear(this.BackColor);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            colorDialog1 = new ColorDialog();
            colorDialog1.ShowDialog();
            c = colorDialog1.Color;
            MessageBox.Show(c.Name);
            CustomizePenColor = new Pen(Color.FromArgb(c.R, c.G, c.B));
        }
    }
}